﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;

namespace FmcWebsite.Core
{
    /// <summary>
    /// Representing a multilanguage content which is represented in a string.
    /// This class can separate the translations.
    /// 
    /// Content is stored in XML format.
    /// </summary>
    public class Multilang
    {
        public string DefaultLang = "EN";

        private IDictionary<string, string> mm_content = new Dictionary<string, string>();

        public Multilang(string content)
        {
            ReadMultilangContent(content);   
        }

        public string this [string lang]
        {
            get
            {
                if (mm_content.ContainsKey(lang.ToUpper()))
                {
                    return mm_content[lang.ToUpper()];
                }
                return "";
            }

            set
            {
                if (mm_content.ContainsKey(lang.ToUpper()))
                {
                    mm_content.Remove(lang.ToUpper());
                }
                mm_content[lang.ToUpper()] = value;
            }
        }

        public string GetAvailable()
        {
            if ((mm_content.ContainsKey(DefaultLang)) && (mm_content[DefaultLang] != ""))
                return mm_content[DefaultLang];
            else
            {
               var avail = mm_content.Values.Where(x => x != "");
               return avail.Count() > 0 ? avail.First() : "";
            }
        }

        public string Value
        {
            get { return ToString(); }
            set { ReadMultilangContent(value); }
        }

        public override string ToString()        
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            StringBuilder builder = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(builder, settings))
            {
                writer.WriteStartElement("MMText");
                foreach (string key in mm_content.Keys)
                {
                    writer.WriteStartElement("Text");
                    writer.WriteAttributeString("Lang", key);
                    writer.WriteString(mm_content[key]);
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
            return builder.ToString();
        }

        private void ReadMultilangContent(string content)
        {
            mm_content.Clear();
            if (content != "")
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(content);
                    var mm_text = doc.GetElementsByTagName("MMText").Item(0);                    
                    foreach (XmlNode txt in mm_text.ChildNodes)
                    {
                        if (txt.Name == "Text")
                        {
                            var lang = txt.Attributes["Lang"].Value;
                            if (lang != "")
                            {
                                mm_content.Add(lang.ToUpper(), txt.InnerText);
                            }
                        }
                    }
                    return;
                }
                catch
                {
                }
            }
            mm_content[DefaultLang] = content;

        }

        static public string Extract(string xml, string language)
        {
            Multilang m = new Multilang(xml);
            return m[language];
        }

        static public string Extract(string xml)
        {
            Multilang m = new Multilang(xml);
            return m.GetAvailable();
        }
        
    }
}
